const router = require('koa-router')()
const middle = require('../middlewares')

const Models = require('../models')
const Workflow = require('../service/workflow')
const Worknote = require('../service/worknote')
const Foundation = require('../service/foundation')

router.use(middle.ensure.user)

/**
 * workflow list
 */
router.get('/', middle.pagination, async(ctx, next) => {
    const { query, state } = ctx
    const { pagination } = state

    const where = {}

    if (query.working) {
        where.locked_by = {
            $not: null
        }
    } else {
        if (query.step) {
            where.step = query.step
        }
    }

    const workflow = await Workflow.pagination(pagination, { where })

    ctx.send({
        workflow,
        pagination
    })
})


router.get('/aggregate', async(ctx, next) => {
    const { query, state } = ctx
    const where = {}


    if (query.working === '1') {
        where.locked_by = {
            $not: null
        }
    }

    if (query.startAt) {

    }

    if (query.endat) {

    }

    const aggregate = await Models.Workflow.count({
        where,
        attributes: [Models.sequelize.col('step'), 'step'],
        group: ['step'],
        raw: true
    })

    ctx.send({
        aggregate
    })
})


router.post('/', async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member
    const { data } = ctx.request.body

    const transaction = await Models.sequelize.transaction()
    try {
        const item = await Models.Item.create({ data }, { transaction })
        const workflow = await Workflow.create(item.id, { transaction })
        const worknote = await Worknote.create({
            user_id: userId,
            workflow_id: workflow.id,
            step: workflow.step,
            transaction_id: transaction.id
        }, { transaction })
        const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})
        await transaction.commit()

        ctx.send({
            workflow
        })
    } catch (err) {
        transaction.rollback()
        throw (err)
    }
})


router.get('/precheck', async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member

    const workflow = await Workflow.preCheck(userId)
    const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})

    ctx.send({
        workflow
    })
})


router.put('/precheck/:id/pass', async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member
    const { id } = ctx.params
    const { data } = ctx.request.body

    const transaction = await Models.sequelize.transaction()
    try {
        const workflow = await Workflow.preCheckPass(id, userId)
        const worknote = await Worknote.create({
            user_id: userId,
            workflow_id: workflow.id,
            step: workflow.step,
            transaction_id: transaction.id
        }, { transaction })
        const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})

        ctx.send({
            workflow
        })

        await transaction.commit()
    } catch (err) {
        transaction.rollback()
        throw (err)
    }
})


router.put('/precheck/:id/reject', async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member
    const { id } = ctx.params
    const { data } = ctx.request.body

    const transaction = await Models.sequelize.transaction()
    try {
        const workflow = await Workflow.preCheckReject(id, userId)
        const worknote = await Worknote.create({
            user_id: userId,
            workflow_id: workflow.id,
            step: workflow.step,
            transaction_id: transaction.id
        }, { transaction })
        const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})

        await transaction.commit()

        ctx.send({
            workflow,
            worknote
        })
    } catch (err) {
        transaction.rollback()
        throw (err)
    }
})


router.get('/complete', async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member
    const { id } = ctx.params
    const { data } = ctx.request.body

    const workflow = await Workflow.complete(userId)
    const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})

    ctx.send({
        workflow
    })
})


router.put('/complete/:id', middle.pagination, async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member
    const { id } = ctx.params
    const { data } = ctx.request.body

    const transaction = await Models.sequelize.transaction()
    try {
        const workflow = await Workflow.completePass(id, userId, data)
        const worknote = await Worknote.create({
            user_id: userId,
            workflow_id: workflow.id,
            step: workflow.step,
            transaction_id: transaction.id
        }, { transaction })
        const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})

        await transaction.commit()

        ctx.send({
            workflow
        })
    } catch (err) {
        transaction.rollback()
        throw (err)
    }
})


router.get('/postcheck', async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member
    const { id } = ctx.params
    const { data } = ctx.request.body

    const workflow = await Workflow.postCheck(userId)
    const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})

    ctx.send({
        workflow
    })
})


router.put('/postcheck/:id/pass', async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member
    const { id } = ctx.params
    const { data } = ctx.request.body

    const transaction = await Models.sequelize.transaction()

    try {
        const workflow = await Workflow.postCheckPass(id, userId)
        const worknote = await Worknote.create({
            user_id: userId,
            workflow_id: workflow.id,
            step: workflow.step,
            transaction_id: transaction.id
        }, { transaction })
        const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})

        await transaction.commit()

        ctx.send({
            workflow
        })
    } catch (err) {
        transaction.rollback()
        throw (err)
    }
})


router.put('/postcheck/:id/reject', async(ctx, next) => {
    const { query, state } = ctx
    const { token, userId } = state.member
    const { id } = ctx.params
    const { data } = ctx.request.body

    const transaction = await Models.sequelize.transaction()

    try {
        const workflow = await Workflow.postCheckReject(id, userId)
        const worknote = await Worknote.create({
            user_id: userId,
            workflow_id: workflow.id,
            step: workflow.step,
            transaction_id: transaction.id
        }, { transaction })
        const snapshot = await Foundation.snapshotCreate(workflow.step, userId, workflow.id, {})

        await transaction.commit()

        ctx.send({
            workflow
        })
    } catch (err) {
        transaction.roolback()
        throw (err)
    }
})


module.exports = router